#ifndef _MAIN_H_
#define _MAIN_H_

#pragma pack(1)

struct power_err_code_bit {
    unsigned char rsvd : 1;
    unsigned char brake : 1;
    unsigned char over_temperature : 1;
    unsigned char below_current : 1;
    unsigned char over_current : 1;
    unsigned char over_volt : 1;
    unsigned char input_err : 1;
    unsigned char output : 1;
};

union power_err_code_all {
    unsigned int              all;
    struct power_err_code_bit bit;
};

struct exec_code1_bit {
    unsigned char ld1_t_err : 1;
    unsigned char ld2_t_err : 1;
    unsigned char ld3_t_err : 1;
    unsigned char ld4_t_err : 1;
    unsigned char pd1_err : 1;
    unsigned char envi_t_err : 1;
    unsigned char envi_h_err : 1;
    unsigned char controller_comm_err : 1;
};

union exec_code1_all {
    unsigned char         all;
    struct exec_code1_bit bit;
};

struct exec_code2_bit {
    unsigned char power1_comm_err : 1;
    unsigned char power2_comm_err : 1;
    unsigned char power3_comm_err : 1;
    unsigned char power4_comm_err : 1;
    unsigned char ld1_current_over : 1;
    unsigned char ld2_current_over : 1;
    unsigned char ld3_current_over : 1;
    unsigned char ld4_current_over : 1;
};

union exec_code2_all {
    unsigned char         all;
    struct exec_code2_bit bit;
};

struct exec_code3_bit {
    unsigned char brake : 1;
    unsigned char rsvd : 7;
};

union exec_code3_all {
    unsigned char         all;
    struct exec_code3_bit bit;
};

struct state_report {
    unsigned char            laser_state;
    unsigned short           power1_volt;
    unsigned short           power1_current;
    unsigned short           power2_volt;
    unsigned short           power2_current;
    unsigned short           power3_volt;
    unsigned short           power3_current;
    unsigned short           power4_volt;
    unsigned short           power4_current;
    unsigned char            intensity_setting;
    short                    ld1_t;
    short                    ld2_t;
    short                    ld3_t;
    short                    ld4_t;
    short                    envi_t;
    unsigned short           envi_h;
    unsigned short           emit_time;
    unsigned short           pd1;
    union power_err_code_all power1_err;
    union power_err_code_all power2_err;
    union power_err_code_all power3_err;
    union power_err_code_all power4_err;
    union exec_code1_all     exec_code1_err;
    union exec_code2_all     exec_code2_err;
    union exec_code3_all     exec_code3_err;
    unsigned char            rsvd_1;
    unsigned char            rsvd_2;
};

struct operate_state {
    volatile unsigned char  laser_state;  // 0:�����  1:�ȴ���  2:����״̬  3:����״̬  4:�쳣״̬
    volatile unsigned short power1_volt;
    volatile unsigned short power1_current;
    volatile unsigned short power2_volt;
    volatile unsigned short power2_current;
    volatile unsigned short power3_volt;
    volatile unsigned short power3_current;
    volatile unsigned short power4_volt;
    volatile unsigned short power4_current;
    volatile unsigned char  intensity_setting;
    volatile float          t1;
    volatile float          t2;
    volatile float          t3;
    volatile float          t4;
    volatile float          envi_t;
    volatile unsigned short envi_h;
    volatile unsigned short emit_time;
    volatile float          pd1;
    volatile float          pd2;
    volatile float          pd3;

    union power_err_code_all power1_err;
    union power_err_code_all power2_err;
    union power_err_code_all power3_err;
    union power_err_code_all power4_err;
    union exec_code1_all     exec_code1_err;
    union exec_code2_all     exec_code2_err;
    union exec_code3_all     exec_code3_err;
    unsigned char            rsvd_1;
    unsigned char            rsvd_2;

    volatile unsigned short power1_setting_current;
    volatile unsigned short power2_setting_current;
    volatile unsigned short power3_setting_current;
    volatile unsigned short power4_setting_current;
};

struct check_feature_bit {
    unsigned int t1 : 1;
    unsigned int t2 : 1;
    unsigned int t3 : 1;
    unsigned int t4 : 1;
    unsigned int envi_t : 1;
    unsigned int envi_h : 1;
    unsigned int pd1 : 1;
    unsigned int ld1_current : 1;
    unsigned int ld2_current : 1;
    unsigned int ld3_current : 1;
    unsigned int ld4_current : 1;
    unsigned int power1_err : 1;
    unsigned int power2_err : 1;
    unsigned int power3_err : 1;
    unsigned int power4_err : 1;
    unsigned int power1_comm : 1;
    unsigned int power2_comm : 1;
    unsigned int power3_comm : 1;
    unsigned int power4_comm : 1;
    unsigned int controller_comm : 1;
    unsigned int brake : 1;
    unsigned int rsvd : 11;
};

union check_feature_all {
    unsigned int             all;
    struct check_feature_bit bit;
};

struct ada_parm {
    union check_feature_all check_feature;
    short                   t1_over_limit;
    short                   t1_low_limit;
    short                   t2_over_limit;
    short                   t2_low_limit;
    short                   t3_over_limit;
    short                   t3_low_limit;
    short                   t4_over_limit;
    short                   t4_low_limit;
    short                   envi_t_over_limit;
    short                   envi_t_low_limit;
    unsigned char           envi_h_over_limit;
    unsigned char           pd1_over_limit;
    unsigned char           pd1_low_limit;
    unsigned short          ld1_current_over_limit;
    unsigned short          ld1_current_low_limit;
    unsigned short          ld2_current_over_limit;
    unsigned short          ld2_current_low_limit;
    unsigned short          ld3_current_over_limit;
    unsigned short          ld3_current_low_limit;
    unsigned short          ld4_current_over_limit;
    unsigned short          ld4_current_low_limit;

    unsigned short ld1_week_current;
    unsigned short ld2_week_current;
    unsigned short ld3_week_current;
    unsigned short ld4_week_current;

    unsigned short current_absolute_over_limit;
    unsigned short current_absolute_low_limit;
    unsigned short current_rate_over_limit;
    unsigned short current_rate_low_limit;

    unsigned short ld1_current_10;
    unsigned short ld1_current_20;
    unsigned short ld1_current_30;
    unsigned short ld1_current_40;
    unsigned short ld1_current_50;
    unsigned short ld1_current_60;
    unsigned short ld1_current_70;
    unsigned short ld1_current_80;
    unsigned short ld1_current_90;
    unsigned short ld1_current_100;

    unsigned short ld2_current_10;
    unsigned short ld2_current_20;
    unsigned short ld2_current_30;
    unsigned short ld2_current_40;
    unsigned short ld2_current_50;
    unsigned short ld2_current_60;
    unsigned short ld2_current_70;
    unsigned short ld2_current_80;
    unsigned short ld2_current_90;
    unsigned short ld2_current_100;

    unsigned short ld3_current_10;
    unsigned short ld3_current_20;
    unsigned short ld3_current_30;
    unsigned short ld3_current_40;
    unsigned short ld3_current_50;
    unsigned short ld3_current_60;
    unsigned short ld3_current_70;
    unsigned short ld3_current_80;
    unsigned short ld3_current_90;
    unsigned short ld3_current_100;

    unsigned short ld4_current_10;
    unsigned short ld4_current_20;
    unsigned short ld4_current_30;
    unsigned short ld4_current_40;
    unsigned short ld4_current_50;
    unsigned short ld4_current_60;
    unsigned short ld4_current_70;
    unsigned short ld4_current_80;
    unsigned short ld4_current_90;
    unsigned short ld4_current_100;
};

#pragma pack()

#define PROGRAM_OFFSET (0x08002000)

void delay_time(unsigned int times);
void memory_set(void *dst, unsigned char data, unsigned int size);
void memory_copy(void *dst, void *src, unsigned int len);

extern struct state_report  state_report_data;
extern struct operate_state operate_parm;

extern struct ada_parm adaptor_parm;
extern struct ada_parm flash_parm;

#endif
